package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.mapper.OpinionaireResultMapper;
import com.hamster.crm.pojo.OpinionaireResult;
import com.hamster.crm.service.OpinionaireResultService;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 问卷调查结果的 Service
 * @author Tarry
 * @create 2019/12/23 13:09
 */
@Service
public class OpinionaireResultServiceImpl implements OpinionaireResultService {
    @Autowired
    private OpinionaireResultMapper opinionaireResultMapper;

    /**
     *  条件查询 问卷结果列表
     * @param pageSize    每页条数
     * @param currentPage 当前页码
     * @param title       标题
     * @param name        员工姓名
     * @return
     */
    @Override
    public PageResult<OpinionaireResult> listOpinionaireResults(Integer pageSize, Integer currentPage, String title, String name) {
        //分页
        Page<OpinionaireResult> page = new Page<>(currentPage, pageSize);
        QueryWrapper<OpinionaireResult> wrapper = new QueryWrapper<>();
        wrapper.select("id","title","create_time","employee_name","opinionaire_id","employee_id")
                .eq("del","0").eq("state","1").orderByDesc("create_time")
                .groupBy("opinionaire_id","employee_id");
        //根据问卷标题过滤
        if (StringUtils.isNotBlank(title)){
            wrapper.like("title","%"+title+"%");
        }
        //根据参与员工姓名过滤
        if (StringUtils.isNotBlank(name)){
            wrapper.like("employee_name","%"+name+"%");
        }

        IPage pageList = opinionaireResultMapper.selectPage(page, wrapper);
        List<OpinionaireResult> currentList = pageList.getRecords();
        Long pages = pageList.getPages();
        return new PageResult<OpinionaireResult>(pageList.getTotal(),pages.intValue(),currentList);

    }

    /**
     * 查询指定员工负责的客户的问卷列表
     * @param opinionaireId 问卷id
     * @param employee_id 员工id
     * @return List<OpinionaireResult>
     */
    @Override
    public List<OpinionaireResult> listDetails(String opinionaireId, String employee_id) {
        QueryWrapper<OpinionaireResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0").eq("state","1").eq("opinionaire_id",opinionaireId)
                .eq("employee_id",employee_id).orderByDesc("submit_time");
        List<OpinionaireResult> list = opinionaireResultMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据问卷结果id  查询问卷结果详情
     * @param id 问卷结果id
     * @return OpinionaireResult 问卷结果详情
     */
    @Override
    public OpinionaireResult getDetail(String id) {
        return opinionaireResultMapper.selectById(id);
    }


}
